package stewardservicehandler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/logic"
	"yundian/internel/app/models/paysetting"
	"yundian/internel/app/models/room"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
	. "yundian/internel/pkg/dingo/result"
)

func HandleSetBatchRoom(ctx *gin.Context) {
	param := &request.SetBatchRoomReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	r := &request.SetBatchDeviceRoomReq{}
	r.PlaceId = param.PlaceId
	for _, lst := range param.RoomRange {
		if lst.Start == 0 && lst.End == 0 {
			continue
		}
		for i := lst.Start; i <= lst.End; i++ {
			p := &request.SetBatchDeviceRoomParamsReq{}
			p.DeviceSn = ""
			p.TypeId = 0
			p.WifiName = fmt.Sprintf("%d", i)
			p.WifiPassword = param.WifiPwd
			p.Room = fmt.Sprintf("%d", i)
			//logs.Info(p.Room,p.WifiPassword,p.WifiName)
			r.List = append(r.List, p)
		}
	}

	data := dao.NewRoomDaoDao().SetBatchPlaceRoomUpdate(r)
	if data.Err != nil {
		Response(ctx)("", CodeInternalServerError, false)(OK)
		return
	}

	Response(ctx)("", CodeSuccess, true)(OK)
}

func HandleSetBatchPlaceRoom(ctx *gin.Context) {
	param := &request.SetBatchDeviceRoomReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.LoginUserId, _ = helper.GetUserIdFromCtx(ctx)

	data := dao.NewRoomDaoDao().SetBatchPlaceRoomUpdate(param)
	if data.Err != nil {
		Response(ctx)("", CodeInternalServerError, false)(OK)
		return
	}

	Response(ctx)("", CodeSuccess, true)(OK)
}

// HandleSetBatchDeviceRoom 批量设置设备房间
func HandleSetBatchDeviceRoom(ctx *gin.Context) {
	param := &request.SetBatchDeviceRoomReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.LoginUserId, _ = helper.GetUserIdFromCtx(ctx)

	data := dao.NewRoomDaoDao().SetBatchDeviceRoom(param)
	if data.Val().(string) == "more" {
		Response(ctx)("暂不支持同时更新多个门店设备", CodeInvalidParam, false)(OK)
		return
	}
	if data.Val().(string) == "devices" {
		Response(ctx)("部分设备绑定不一致/未绑定", CodeInvalidParam, false)(OK)
		return
	}

	Response(ctx)("", CodeSuccess, true)(OK)
}

func HandleSetRoomOrderStatus(ctx *gin.Context) {
	param := &request.SetRoomOrderStatusReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.LoginUserId, _ = helper.GetUserIdFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewRoomDaoDao().GetAdminRoomOrdersInfo(param).Unwrap().(*room.Order)
	if data.Id <= 0 {
		Response(ctx)("", CodeInvalidParam, false)(OK)
		return
	}

	// 执行退款
	if data.Status != helper.TypeNumRoomOrderStatusRepeal && data.PayType == 1 && param.Status == helper.TypeNumRoomOrderStatusRepeal {
		if err := logic.NewOrderLogic().HandleWxStewardCancelOrder(&paysetting.WxStewardCancelOrder{PathKey: "room",
			TransactionId: data.ThirdTradeNo, TradeNo: data.TradeNo, Amount: int64(data.Amount)}); err != nil {
			Response(ctx)("", CodeInvalidParam, false)(OK)
			return
		}
	}

	if dao.NewRoomDaoDao().SetAdminRoomOrdersStatus(data, param).Err != nil {
		Response(ctx)("", CodeInvalidParam, false)(OK)
		return
	}

	Response(ctx)("", CodeSuccess, true)(OK)
}

// HandleGetStewardRoomOrderList 房间订单列表
func HandleGetStewardRoomOrderList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.RoomOrderListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.LoginUserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewRoomDaoDao().GetRoomOrderList(param)

	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       data.Unwrap(),
	})(OK)
}

// HandleGetStewardRoomTypeNotPagingList 房间类型列表
func HandleGetStewardRoomTypeNotPagingList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.RoomTypeListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.LoginUserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.NotPagingStatus = 2

	data := dao.NewRoomDaoDao().GetRoomTypeList(param)

	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleGetStewardRoomTypeList 房间类型列表
func HandleGetStewardRoomTypeList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.RoomTypeListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.LoginUserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)

	data := dao.NewRoomDaoDao().GetRoomTypeList(param)

	Response(ctx)("", CodeSuccess, ResponseConfigList{
		Host:       global.Cfg.Ali.AliossPath,
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       data.Unwrap(),
	})(OK)
}

// HandleGetStewardRoomTypeInsert 房间类型新增
func HandleGetStewardRoomTypeInsert(ctx *gin.Context) {
	param := &room.TypeDb{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	if data := dao.NewRoomDaoDao().InsertRoomType(param); data.Err != nil {
		Response(ctx)(data.Err.Error(), CodeSuccess, false)(OK)
		return
	}

	Response(ctx)("", CodeSuccess, true)(OK)
}

// HandleGetStewardRoomTypeUpdate 房间类型修改
func HandleGetStewardRoomTypeUpdate(ctx *gin.Context) {
	param := &room.TypeDb{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	if data := dao.NewRoomDaoDao().UpdateRoomType(param); data.Err != nil {
		Response(ctx)(data.Err.Error(), CodeSuccess, false)(OK)
		return
	}

	Response(ctx)("", CodeSuccess, true)(OK)
}

// HandleGetStewardRoomTypeDelete 房间类型删除
func HandleGetStewardRoomTypeDelete(ctx *gin.Context) {
	param := &room.TypeDb{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	if data := dao.NewRoomDaoDao().DeleteRoomType(param); data.Err != nil {
		Response(ctx)(data.Err.Error(), CodeSuccess, false)(OK)
		return
	}

	Response(ctx)("", CodeSuccess, true)(OK)
}
